import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/main_page/home/detail/page/commit_all_page.dart';
import 'package:flutter_douban/model/note_content_model.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_douban/tool_views/cached_network_Image.dart';

class CommitCell extends StatefulWidget {
  final NoteContentModel model;
  final List? commotList;
  final int? index;
  const CommitCell({Key? key, required this.model, this.commotList, this.index})
      : super(key: key);

  @override
  _CommitCellState createState() => _CommitCellState();
}

class _CommitCellState extends State<CommitCell> {
  @override
  Widget build(BuildContext context) {
    List<Widget> widgetList = [];
    if (widget.commotList!.length == widget.index) {
      widgetList.add(
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _avatar(),
            _btn(),
          ],
        ),
      );
      widgetList.add(
        Container(
          color: Colors.white,
          padding: EdgeInsets.only(left: 43, right: 20, top: 4),
          child: Text(
            widget.model.content![0].c!,
            style: TextStyle(color: Colors.black),
          ),
        ),
      );
      widgetList.add(
        InkWell(
          child: Container(
            alignment: Alignment.center,
            width: MediaQuery.of(context).size.width,
            height: 40,
            margin: EdgeInsets.only(
              top: 10,
              bottom: 0,
            ),
            child: Text(
              '查看全部评论',
              style: TextStyle(
                  color: Color.fromRGBO(94, 146, 140, 1),
                  fontWeight: FontWeight.bold),
            ),
          ),
          onTap: () {
            CustomShareView.showShareView(context, widget.commotList!);
          },
        ),
      );
    } else {
      widgetList.add(
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _avatar(),
            _btn(),
          ],
        ),
      );
      widgetList.add(
        Container(
          padding: EdgeInsets.only(left: 43, right: 20, top: 4),
          child: Text(
            widget.model.content![0].c!,
            style: TextStyle(color: Colors.black),
          ),
        ),
      );
    }

    return Container(
      padding: EdgeInsets.only(top: 10, bottom: 10),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgetList,
      ),
    );
  }

  /// 头像
  Widget _avatar() {
    return Row(
      children: [
        IconImage(
            url: widget.model.u!.length == 0
                ? 'https://img1.chexun.com/chexunimg/erpimg/2020/0103/icon_default_d7578fdff611473b9f1b6af3155d06aa.jpg'
                : widget.model.u!.split(",")[5].substring(4),
            width: 34,
            height: 34,
            radius: 17,
            borderWidth: 1,
            borderColor: AppColors.line),
        SizedBox(
          width: 8,
        ),
        _centerview(),
      ],
    );
  }

  /// 中间内容
  Widget _centerview() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: 4,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Text(
              widget.model.u!.length == 0 ? '木子雨廷' :widget.model.u!.split(",")[1].substring(4),
              // '郭德纲',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 13,
                fontWeight: FontWeight.bold,
                color: AppColors.black,
              ),
            ),
            SizedBox(
              width: 8,
            ),
            Text(
              widget.model.u!.length == 0 ? 'LV.18' : 'LV.${widget.model.u!.split(",")[6].substring(6)}',
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  fontSize: 11,
                  color: AppColors.yellow,
                  fontStyle: FontStyle.italic,
                  fontWeight: FontWeight.bold),
            ),
          ],
        ),
        Text(
          widget.model.at!,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontSize: 12,
            color: AppColors.qianTextColor,
          ),
        ),
      ],
    );
  }

  /// 关注按钮
  Widget _btn() {
    return SizedBox(
      width: 70,
      height: 28,
      child: TextButton(
        onPressed: () => print(''),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Image.asset(
              'assets/images/common/bottomtool_unlike.png',
              width: 14,
              height: 14,
            ),
            SizedBox(
              width: 4,
            ),
            Text(
              '赞',
              style: TextStyle(
                color: AppColors.qianTextColor,
                fontSize: 13,
                fontWeight: FontWeight.w400,
              ),
            )
          ],
        ),
        // color: Colors.white,
      ),
    );
  }
}
